<template>
	<view class="agencies_wrap">
		<view class="index-bg agencies_backg" style="height: 460rpx;"></view>
		<view class="agencies_my">
			<view class="agencies_my_icons">
				<image class="agencies_my_icon" style="margin-right: 62rpx;"
					@click="gotoRouter('/pages-agencies/agenciesSet/agenciesSet')"
					src="https://res.jiuzhang-cloud.com/jzy-oss/upload/image/dti-hospital/agencies/agencies_set.png">
				</image>
				<image class="agencies_my_icon" @click="scanCode"
					src="https://res.jiuzhang-cloud.com/jzy-oss/upload/image/dti-hospital/agencies/agencies_scan.png">
				</image>
			</view>
			<view class="agencies_my_imgtxt">
				<view class="agencies_my_img">
					<image :src="agencies_data.doorPic"></image>
				</view>
				<view class="agencies_my_txt">
					<view class="agencies_my_dispensary">{{ agencies_data.name }}</view>
					<view class="agencies_my_namechange">
						<view class="agencies_my_name">{{ agencies_data.conName }} {{ agencies_data.conTel }}</view>
					</view>
					<view class="agencies_my_address">{{ agencies_data.provinceName == agencies_data.cityName ? '' :
							(agencies_data.provinceName + '-')
					}}{{ agencies_data.cityName }}-{{
		agencies_data.areaName
}}-{{ agencies_data.address }}</view>
				</view>
			</view>
		</view>
		<view class="agencies_data">
			<view class="agencies_data_num">
				<view>注册人数</view>
				<view>{{ agencies_data.dayOrderCount }}</view>
			</view>
			<view class="agencies_vertical"></view>
			<view class="agencies_data_num">
				<view>今日付款件数</view>
				<view>{{ agencies_data.dayProductCount }}</view>
			</view>
			<view class="agencies_vertical"></view>
			<view class="agencies_data_num">
				<view>今日付款金额</view>
				<view>{{ agencies_data.dayPayMoney }}</view>
			</view>
		</view>
		<view class="member-main">
			<u-title value="资产管理"></u-title>
			<view class="member-main-list">
				<view class="member-main-item" @click="gotoRouter('/pages-agencies/payments/payments')">
					<image class="main-item-pic"
						src="https://res.jiuzhang-cloud.com/jzy-oss/upload/image/dti-hospital/agencies/agencies_money.png">
					</image>
					<view class="main-item-info">收支记录</view>
				</view>
				<view class="member-main-item"
					@click="gotoRouter('/pages-agencies/withdrawList/withdrawList?busType=10')">
					<image class="main-item-pic"
						src="https://res.jiuzhang-cloud.com/jzy-oss/upload/image/dti-hospital/agencies/agencies_extracting.png">
					</image>
					<view class="main-item-info">提现记录</view>
				</view>
				<view class="member-main-item" @click="gotoRouter('/pages-agencies/clearingOff/clearingOff')">
					<image class="main-item-pic"
						src="https://res.jiuzhang-cloud.com/jzy-oss/upload/image/dti-hospital/agencies/agencies_notrecord.png">
					</image>
					<view class="main-item-info">未结算记录</view>
				</view>
				<view class="member-main-item" @click="gotoRouter('/pages-agencies/clearing/clearing')">
					<image class="main-item-pic"
						src="https://res.jiuzhang-cloud.com/jzy-oss/upload/image/dti-hospital/agencies/agencies_haveRecording.png">
					</image>
					<view class="main-item-info">已结算记录</view>
				</view>
			</view>
			<view class="available_surplus">
				<view>账户可用余额（元）</view>
				<view>{{ agencies_data.usableMoney }}</view>
				<view><button class="available_surplus_button"
						@click="gotoRouter('/pages-agencies/WithdrawRequest/WithdrawRequest?amount=' + agencies_data.usableMoney + '&busType=10')">提现</button>
				</view>
			</view>
		</view>
		<view class="member-main">
			<u-title value="机构分享管理"></u-title>
			<view class="member-main-list">
				<view class="member-main-item"
					@click="gotoRouter('/pages-agencies/withdrawList/withdrawList?busType=24')">
					<image class="main-item-pic"
						src="https://res.jiuzhang-cloud.com/jzy-oss/upload/image/dti-hospital/agencies/agencies_extracting.png">
					</image>
					<view class="main-item-info">提现记录</view>
				</view>
				<view class="member-main-item" @click="gotoRouter('/pages-agencies/inviteList/inviteList')">
					<image class="main-item-pic"
						src="https://res.jiuzhang-cloud.com/jzy-oss/upload/image/dti-hospital/agencies/agencies_invitedlist.png">
					</image>
					<view class="main-item-info">邀请列表</view>
				</view>
				<view class="member-main-item" @click="showOrHideQRCode()">
					<image class="main-item-pic"
						src="https://res.jiuzhang-cloud.com/jzy-oss/upload/image/dti-hospital/agencies/agencies_invite.png">
					</image>
					<view class="main-item-info">邀请机构</view>
				</view>
			</view>
			<view class="datas_analyze">
				<view>
					<view>注册人数</view>
					<view>{{ healthy_data.registerCount }}</view>
				</view>
				<view>
					<view>销售数量</view>
					<view>{{ healthy_data.payCount }}</view>
				</view>
				<view>
					<view>消费金额</view>
					<view>{{ healthy_data.payMoneyTotal }}</view>
				</view>
				<view>
					<view>总计佣金</view>
					<view>{{ healthy_data.disMoneyTotal }}</view>
				</view>
			</view>
			<view class="available_surplus">
				<view>账户可用余额（元）</view>
				<view>{{ healthy_data.usableMoney }}</view>
				<view><button class="available_surplus_button"
						@click="gotoRouter('/pages-agencies/WithdrawRequest/WithdrawRequest?amount=' + healthy_data.usableMoney + '&busType=24')">提现</button>
				</view>
			</view>
		</view>
		<view class="member-main">
			<u-title value="资产管理"></u-title>
			<view class="asset_manage">
				<view class="asset_div">
					<view>网址：{{ url }}</view>
					<view class="url_copy" @click="url_copy()">复制</view>
				</view>
				<view class="asset_div">
					<view>账号：{{ agencies_data.loginNo }}</view>
				</view>
				<view class="asset_div bord_bot">
					<view>密码：{{ password_switch ? agencies_data.loginPwd : '*******' }}</view>
					<view>
						<image v-if="password_switch" @click="password_on"
							src="https://res.jiuzhang-cloud.com/jzy-oss/upload/image/dti-hospital/agencies/password_hide.png">
						</image>
						<image v-else @click="password_off"
							src="https://res.jiuzhang-cloud.com/jzy-oss/upload/image/dti-hospital/agencies/password_emerge.png">
						</image>
					</view>
				</view>
			</view>

		</view>
		<view class="agencies_my_change" @click="gotoRouter('/pages/accountSwitch/accountSwitch?pageType=3')">切换账号
		</view>
		<view v-if="qrcodeShow" class="qrcode-root">
			<view class="qrcode-dialog">
				<image class="close-btn"
					src="https://res.jiuzhang-cloud.com/jzy-oss/upload/image/dti-hospital/ic_float_close.png"
					@click="showOrHideQRCode()"></image>
					<view class="poster-wrap">
						<l-painter ref="painter">
							<l-painter-view css="width: 568rpx; box-sizing:border-box; background: #ffffff; align-items: center; padding: 24rpx; border-radius: 24rpx;">
								<l-painter-image src="https://res.jiuzhang-cloud.com/jzy-oss/upload/image/dti-hospital/ikon@2x.png" css="width: 480rpx; height: 332rpx; margin: 24rpx auto; border-radius: 24rpx;"/>
								<l-painter-text css="font-size: 32rpx; color: #666666; margin: 24rpx 0" text="邀请您一起加入药店平台"></l-painter-text>
								<l-painter-view css="display:block; height: 2rpx; background: #E5E5E5; margin: 0;"></l-painter-view>
								<l-painter-view css="display:flex; align-items:center; margin: 24rpx;">
									<l-painter-image css="width: 88rpx; height: 88rpx; border-radius: 44rpx; margin-right: 16rpx" :src="userInfo.avatar"></l-painter-image>
									<l-painter-view css="flex:1; box-sizing:border-box;">
										<l-painter-text css="font-size: 32rpx; color: #222222; font-weight: 500; display:block;" :text="userInfo.username"></l-painter-text>
										<l-painter-text css="font-size: 24rpx; color: #999999;" text="长按识别二维码"></l-painter-text>
									</l-painter-view>
									<l-painter-qrcode :text="qrcodeContent" css="width: 120rpx;height: 120rpx;"></l-painter-qrcode>
								</l-painter-view>
							</l-painter-view>
						</l-painter>
					</view>
					<view class="btn-line">
						<button class="confirm-btn" open-type="share">微信分享</button>
						<view class="confirm-btn" @click="saveQRCode()">保存至相册</view>
					</view>
			</view>
		</view>
	</view>
</template>

<script>
import { userInfo } from '@/utils/token.js';
import { apiUrl } from '@/config.js'
export default {
	data() {
		return {
			url: apiUrl+'drug/login',
			password_switch: false,
			agencies_data: {
				address: "",
				areaName: "",
				cityName: "",
				conName: "",
				conTel: "",
				dayOrderCount: 0,
				dayPayMoney: 0,
				dayProductCount: 0,
				doorPic: "",
				loginNo: "",
				loginPwd: "",
				name: "",
				provinceName: "",
				usableMoney: ""
			},//首页数据
			healthy_data: {
				disMoneyTotal: "",
				hasWithdrawMoney: "",
				payCount: 0,
				payMoneyTotal: "",
				registerCount: 0,
				usableMoney: "",
			},
			userInfo: null,
			qrcodeContent: '',
			qrcodeShow: false
		}
	},
	onLoad(option) {
		// 此生命周期用来获取
		this.queryMpInfo();
	},
	onShow() {
		this.userInfo = userInfo.get();
		if (this.userInfo) {
			this.qrcodeContent = 'https://disugroup.com/qrcode?scene=' + 'agency_invite&id=' + this.userInfo.id;
		}
		this.getIndexInfo()
		this.getHealthyMoney()
	},
	onShareAppMessage(res) {
		if (res.from === 'button') {
			return {
				title: '邀请您一起加入药店平台',
				path: this.qrcodeContent.replace('https://disugroup.com/qrcode',
							'/pages-mall/qrcodeHandler/qrcodeHandler'),
				imageUrl: 'https://res.jiuzhang-cloud.com/jzy-oss/upload/image/dti-hospital/ikon@2x.png'
			}
		}
	},
	methods: {
		queryMpInfo() {
			this.$api.getMpConfig().then(res => {
				if (res.code == 200) {
					if (res.result.contentInfo.drugUrl) {
						this.url = res.result.contentInfo.drugUrl;
					}
				}
			})
		},
		// 网址复制
		url_copy() {
			uni.setClipboardData({
				data: this.url
			});
		},
		// 密码的显示/关闭
		password_off() {
			this.password_switch = true
		},
		password_on() {
			this.password_switch = false
		},
		// 扫码
		scanCode() {
			var that = this;
			uni.scanCode({
				success: function (res) {
					that.gotoRouter(res.result.replace('https://disugroup.com/qrcode', '/pages-mall/qrcodeHandler/qrcodeHandler'));
				}
			});
		},
		// 机构首页数据
		getIndexInfo() {
			this.$api.getIndexInfo().then(e => {
				const { code, result } = e
				if (code == 200) {
					this.agencies_data = result
				} else {
					uni.showToast({
						title: e.message,
						icon: 'none'
					});
				}
			})
		},
		// 机构首页健康金
		getHealthyMoney() {
			this.$api.getHealthyMoney().then(e => {
				const { code, result } = e
				if (code == 200) {
					this.healthy_data = result
				} else {
					uni.showToast({
						title: e.message,
						icon: 'none'
					});
				}
			})
		},

		showOrHideQRCode() {
			this.qrcodeShow = !this.qrcodeShow;
		},

		saveQRCode() {
			uni.showLoading({
				title: '保存中',
				mask: true
			});
			this.$refs.painter.canvasToTempFilePath({
			  fileType: "jpg",
			  // 如果返回的是base64是无法使用 saveImageToPhotosAlbum，需要设置 pathType为url
			  pathType: 'url',
			  quality: 1,
			  success: (res) => {
				uni.showToast({
					icon: 'success',
					title: '保存成功'
				});
			    // 非H5 保存到相册
			    // H5 提示用户长按图另存
			    uni.saveImageToPhotosAlbum({
			        filePath: res.tempFilePath,
			        success: function () {
			            console.log('save success');
			        }
			    });
			  },
			  complete: () => {
				  uni.hideLoading();
			  }
			});
		}
	}
}
</script>

<style lang="less">
@import './agencies.less';
</style>
